import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Cookies from 'js-cookie'
class HttpAxios {
  constructor () {
    // 要修改的地方
    // 1.公共请求地址
    this.baseURL = 'http://8.142.74.15:9091/'
  }

  // 拦截器配置
  SetInterceptors = (instance) => {
    // 请求拦截器
    instance.interceptors.request.use(config => {
      // console.log(config)
      // 请求开始进度条
      NProgress.start()
      // 2.根据需求设置请求头  设置token
      config.headers.Authorization = sessionStorage.getItem('token')
      config.headers.token = (`${sessionStorage.getItem('token')}`)
      // 必须要return
      return config
    }, err => Promise.reject(err))
    // 响应拦截器
    instance.interceptors.response.use(response => {
      console.log('我是响应拦截器')
      // 请求完成进度条
      NProgress.done()
      // console.log(response)
      return response
    }, err => Promise.reject(err))
  }

  request (options) {
    // 创建axios实例
    const instance = axios.create()
    const config = {
      ...options,
      baseURL: this.baseURL
    }
    this.SetInterceptors(instance)
    return instance(config)
  }
}

export default new HttpAxios()
